using DbJobsApi.Extensions;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Data.Sqlite;

//var ApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//var apppath = Path.Combine(ApplicationData, "dbjobs");
//if (!Directory.Exists(apppath))
//{
//    Directory.CreateDirectory(apppath);
//}
//var dbpath = Path.Combine(apppath, "data.db");
//if (!Directory.Exists(apppath))
//    System.IO.File.WriteAllText(dbpath, String.Empty);
//using var conn = new SqliteConnection($"Data Source={dbpath};");
//conn.SqlteExcute(@"
//CREATE TABLE IF NOT EXISTS conn(
//   --id   INTEGER PRIMARY KEY AUTOINCREMENT,
//   name TEXT PRIMARY KEY NOT NULL,
//   data TEXT NOT NULL
//);
//");


var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var services = builder.Services;

services.AddControllers();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging(builder =>
{
    builder.ClearProviders();
    builder.AddSimpleConsole(b => b.SingleLine = true);
});

//options => options.Filters.Add<GlobalExceptionsFilter>()
services.AddControllers()
        .AddNewtonsoftJson(options =>
        {
            options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
            options.SerializerSettings.DateFormatString = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
        });

var app = builder.Build();

//app.MapControllers();
app.MapControllerRoute(
     name: "default",
     pattern: "api/{controller=Home}/{action=Index}/{id?}");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/check", async context =>
    {
        await context.Response.WriteAsync($"Health Ckeck {System.DateTime.Now}");
    });
});
app.UseStaticFiles();

app.Run();
